/*********************************************************
 *
 *  Project:      HZFlashCards
 *
 *  Class:        HTMLListPane
 *
 *  File name:    HTMLListPane.java
 *
 *
 *    SVN:
 *
 *    $Rev$:
 *    $Date$:
 *    $LastChangedBy$:
 *
 *    $HeadURL$:
 *
 **********************************************************/

package com.eastmountain.chinese.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

//import org.jvnet.substance.SubstanceLookAndFeel;

import java.util.Vector;

import com.eastmountain.chinese.common.HTMLStrings;
import com.eastmountain.chinese.db.flashcards.records.ChineseElementProperties;

/*******************************************************************************
 * 
 * Class: TableReviewListPane
 * 
 ******************************************************************************/

@SuppressWarnings("serial")
public class TableReviewListPane extends HTMLListPane {

	private Vector<ChineseElementProperties> chineseElementReviewList;

	private String[][] fieldValues;
	private static String[] tdClasses = { "center", "center", "left", "right",
			"right" };

	public TableReviewListPane() {

		// TODO take care of word review list

		System.out.println("\nTableReviewListPane() invoked\n");
		new SimpleTableSelectionDemo();

	}

	public void displayHTML(
			Vector<ChineseElementProperties> chineseElementReviewList) {

		System.out.println("\nHTMLReviewListPane.displayHTML() invoked\n");

		this.chineseElementReviewList = chineseElementReviewList;

		createHTMLListPanel("Review List");

		fieldValues = extractFieldValues(chineseElementReviewList);

		String trTag = "<tr>";
		String tdTag = "<td>";
		String tableData;
		int i1 = 0;
		int i2 = 0;

		startHTML();

		htmlBody.append("<table id=\"mytable\">" + HTMLStrings.nl + "<tbody>"
				+ HTMLStrings.nl + "<caption>Character Review List</caption>"
				+ HTMLStrings.nl);

		htmlBody.append("<tr>" + HTMLStrings.nl
				+ "  <th class=\"narrow\">Character</th>" + HTMLStrings.nl
				+ "  <th class=\"narrow\">PinYin</th>" + HTMLStrings.nl
				+ "  <th class=\"definition\">Definition</th>" + HTMLStrings.nl
				+ "  <th class=\"narrow\">Lesson</th>" + HTMLStrings.nl
				+ "  <th class=\"narrow\">Level</th>" + HTMLStrings.nl
				+ "</tr>" + HTMLStrings.nl);

		i1 = 0;

		for (@SuppressWarnings("unused")
		ChineseElementProperties chineseElementProperties : chineseElementReviewList) {

			if (i1 % 2 == 0) {

				trTag = "<tr class=\"alt\">";

			} else {

				trTag = "<tr>";

			}

			htmlBody.append(trTag);

			for (i2 = 0; i2 < ChineseElementProperties.NUMBER_OF_FIELD_VALUES; i2++) {

				tdTag = "    <td class=\"" + TableReviewListPane.tdClasses[i2]
						+ "\">";

				tableData = tdTag + fieldValues[i1][i2] + "</td>"
						+ HTMLStrings.nl;
				htmlBody.append(tableData);

			}

			htmlBody.append("</tr>" + HTMLStrings.nl);

			i1++;

		}

		htmlBody.append("</tbody>" + HTMLStrings.nl + "</table>"
				+ HTMLStrings.nl);

		finishHTML();

	}

	private String[][] extractFieldValues(
			Vector<ChineseElementProperties> chineLessonFlashCardSet) {

		String[][] fieldValues = new String[chineLessonFlashCardSet.size()][ChineseElementProperties.NUMBER_OF_FIELD_VALUES];
		String lessonNumber = "";

		for (int i = 0; i < chineLessonFlashCardSet.size(); i++) {

			fieldValues[i][0] = chineLessonFlashCardSet.elementAt(i)
					.getChineseElement();
			fieldValues[i][1] = chineLessonFlashCardSet.elementAt(i)
					.getPinyin();
			fieldValues[i][2] = chineLessonFlashCardSet.elementAt(i)
					.getEnglish();
			lessonNumber = Integer.toString(chineLessonFlashCardSet
					.elementAt(i).getLessonNumber());
			fieldValues[i][3] = lessonNumber;
			fieldValues[i][4] = chineLessonFlashCardSet.elementAt(i).getLevel();
		}

		return fieldValues;

	}

}

/*******************************************************************************
 * 
 * Project: Examples
 * 
 * Class: SimpleTableSelectionDemo
 * 
 * File name: SimpleTableSelectionDemo.java
 * 
 * 
 * SVN:
 * 
 * $Rev$: $Date$: $LastChangedBy$:
 * 
 * $HeadURL$:
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 * Class: SimpleTableSelectionDemo
 * 
 ******************************************************************************/

/*
 * SimpleTableSelectionDemo.java requires no other files.
 */

/**
 * SimpleTableSelectionDemo is just like SimpleTableDemo, except that it detects
 * selections, printing information about the current selection to standard
 * output.
 */

class SimpleTableSelectionDemo extends JPanel {
	
	private boolean DEBUG = false;
	private boolean ALLOW_COLUMN_SELECTION = true;
	private boolean ALLOW_ROW_SELECTION = true;

	public SimpleTableSelectionDemo() {
		super(new GridLayout(1, 0));

		final String[] columnNames = { "Chinese", "Last Name", "Sport",
				"# of Years", "Vegetarian" };

		final Object[][] data = {
				{ "\u65e5", "Campione", "Snowboarding", new Integer(5),
						new Boolean(false) },
				{ "\u672c", "Huml", "Rowing", new Integer(3), new Boolean(true) },
				{ "\u5217", "Walrath", "Knitting", new Integer(2),
						new Boolean(false) } };

		final JTable table = new JTable(data, columnNames);

		// table.getModel()
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));

		Font font = new Font("Arial Unicode MS", Font.PLAIN, 16);

		setFont(font);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		if (ALLOW_ROW_SELECTION) { // true by default
			ListSelectionModel rowSM = table.getSelectionModel();
			rowSM.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					// Ignore extra messages.
					if (e.getValueIsAdjusting()) {
						return;
					}

					ListSelectionModel lsm = (ListSelectionModel) e.getSource();
					if (lsm.isSelectionEmpty()) {
						System.out.println("No rows are selected.");
					} else {
						int selectedRow = lsm.getMinSelectionIndex();
						System.out.println("Row " + selectedRow
								+ " is now selected.");
					}
				}
			});
		} else {
			table.setRowSelectionAllowed(false);
		}

		if (ALLOW_COLUMN_SELECTION) { // false by default
			if (ALLOW_ROW_SELECTION) {
				// We allow both row and column selection, which
				// implies that we *really* want to allow individual
				// cell selection.
				table.setCellSelectionEnabled(true);
			}
			table.setColumnSelectionAllowed(true);
			ListSelectionModel colSM = table.getColumnModel()
					.getSelectionModel();
			colSM.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					// Ignore extra messages.
					if (e.getValueIsAdjusting()) {
						return;
					}

					ListSelectionModel lsm = (ListSelectionModel) e.getSource();
					if (lsm.isSelectionEmpty()) {
						System.out.println("No columns are selected.");
					} else {
						int selectedCol = lsm.getMinSelectionIndex();
						System.out.println("Column " + selectedCol
								+ " is now selected.");
					}
				}
			});
		}

		if (DEBUG) {
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					printDebugData(table);
				}
			});
		}

		// Create the scroll pane and add the table to it.
		JScrollPane scrollPane = new JScrollPane(table);

		// Add the scroll pane to this panel.
		add(scrollPane);
	}

	private void printDebugData(JTable table) {
		int numRows = table.getRowCount();
		int numCols = table.getColumnCount();
		javax.swing.table.TableModel model = table.getModel();

		System.out.println("Value of data: ");
		for (int i = 0; i < numRows; i++) {
			System.out.print("    row " + i + ":");
			for (int j = 0; j < numCols; j++) {
				System.out.print("  " + model.getValueAt(i, j));
			}
			System.out.println();
		}
		System.out.println("--------------------------");
	}

	public static void main(String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				// createAndShowGUI();
			}
		});
	}

}
